package com.njtech.pet.controller;

import com.alibaba.fastjson.JSON;
import com.njtech.pet.constant.Constant;
import com.njtech.pet.entity.Person;
import com.njtech.pet.entity.User;
import com.njtech.pet.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:gm
 * Date:2020-03-31 23:32
 * Description:<自然人信息维护>
 */
@Controller
@RequestMapping("/person")
public class PersonController {
    @Autowired
    PersonService personService;

    /**
     * 功能：所有自然人信息
     * @return  信息集合
     */
    @RequestMapping("/findAllPerson")
    @ResponseBody
    public String findAllPerson(@SessionAttribute(value = "user") User user){
        if(user==null){
            return null;
        }
        List<Person> personList = personService.selectByUserId(user);
        System.out.println(personList);
        return JSON.toJSONString(personList);
    }

    /**
     * 功能：增添一个自然人
     * @return   提示信息
     */
    @RequestMapping("/addPerson")
    @ResponseBody
    public String addPerson(String personName,String idType,String idendity,String phone,String email,String relation,String remark,HttpSession session){
        Map<String,Object> resultMap = new HashMap<>();
        Person person = new Person(null, personName, idType, idendity, phone, email, null, Constant.Person.STATE_VALID.getValue(), null, relation, remark);
        //设置用户
        User user =  (User)session.getAttribute("user");
        person.setUser(user);
        int flag = personService.insert(person);
        if (flag==1){
            resultMap.put("msg","新增一条自然人成功！");
        }else{
            resultMap.put("msg","新增一条自然人失败！");
        }
        return JSON.toJSONString(resultMap);
    }

    /**
     * 功能：更改自然人信息
     * @return 提示信息
     */
    @RequestMapping("/updatePerson")
    @ResponseBody
    public String updatePerson(Integer personId,String personName,String idType,String idendity,String phone,String email,String relation,String remark,String isValue){
        Map<String,Object> resultMap = new HashMap<>();
        Person person = new Person(personId, personName, idType, idendity, phone, email, null,isValue, null, relation, remark);
        int flag = personService.updateByPersonId(person);
        if (flag==1){
            resultMap.put("msg","修改一条自然人成功！");
        }else{
            resultMap.put("msg","修改一条自然人失败！");
        }
        return JSON.toJSONString(resultMap);
    }

    /**
     * 删除自然人
     * @param personId  自然人id
     * @return          提示信息
     */
    @RequestMapping("/deletePerson")
    @ResponseBody
    public String deletePerson(Integer personId){
        if(personId==null){
            return null;
        }
        Map<String,Object> resultMap = new HashMap<>();
        Person person = new Person();
        person.setId(personId);
        person.setState(Constant.Person.STATE_INVALID.getValue());
        int flag = personService.updateByPersonId(person);
        System.out.println(flag);
        if (flag==1){
            resultMap.put("msg","删除一条自然人成功！");
        }else{
            resultMap.put("msg","删除一条自然人失败！");
        }
        return JSON.toJSONString(resultMap);
    }

    /*****************************
     * 功能4：查询一条自然人信息
     * @param id  自然人id
     * @return   结果集
     * ------------------------成功
     */
    @RequestMapping(value={"/selectPerson"})
    @ResponseBody
    public String selectPerson(@RequestParam Integer id){
        if(id==null){
            return null;
        }
        Person person = personService.selectByPersonId(id);
        return JSON.toJSONString(person);
    }
}
